dojo.require("simp.RegionSelector");

dojo.provide("CropEditor");
dojo.declare("CropEditor", BaseEditor, {
	editorMarkup: "<h1>Crop</h1>",
	imageRegionId: "imageRegion",
	regionSelector: null,
	preset: null,
	image: null,
	_commitEvent: null,
	_loaded: false,
	
	constructor: function() {
		console.debug("Crop Editor created");
		this._moduleName = "crop";
		ctrl.registerEditor(this)
		ctrl.getEditor(this._moduleName, this.setEditor);
	},
	
	setEditor: function(result) {
		console.debug("crop editor markup loaded");
		ctrl.editors.item("crop").editorMarkup = result.editor;
	},
	
	getEditorMarkup: function(result) {
		return this.editorMarkup
	},
	
	activate: function(image) {
		console.debug("Cropper activation");
		this.deactivate();
		this.regionSelector = dijit.byId(this.imageRegionId);
		//this.regionSelector = dijit.byId("imageRegion")
		this._commitEvent = dojo.connect(this.regionSelector, "onRegionDblClick", this, "onCommit");
		//console.debug("regionSelector: " + dojo.debugReturn(this.regionSelector))
		this.image = dojo.byId("imageContainer");	
		if (image != null) {
			this.image.src = image.url;
			dojo.style(this.image, "height", image.height + 'px');
			dojo.style(this.image, "width", image.width + 'px');
			dojo.style(this.regionSelector.domNode, "height", image.height + 'px');
			dojo.style(this.regionSelector.domNode, "width", image.width + 'px');
		}
		console.debug("Cropper activated");	
	},
	
	deactivate: function() {
		dojo.disconnect(this._commitEvent);
	},
	
	setValues: function(values) {
		console.debug("CropEditor setValues: "+ dojo.debugReturn(values));// "Cropper is: " + dojo.debugReturn(this));
		if (this.regionSelector != null) {
			console.debug("CropEditor: setup regionSelector");
			this.regionSelector.holdAspectRatio = values.hold_aspect;
			this.regionSelector.minHeight = values.min.h;
			this.regionSelector.minWidth = values.min.w;
			this.regionSelector.showHandles = values.show_handle;
			this.regionSelector.setCoords(values.coords);
		} 
		else { 
			this.activate();
			//this.setValues(values);
			console.debug("Cropper - regionSelector isn't definied")
		}
	},
	
	editorLoaded: function() {
		if (!this._loaded){
			console.debug("CtropEditor Init");
			dojo.disconnect(this._commitEvent);
			this._commitEvent = dojo.connect(this.regionSelector, "onRegionDblClick", this, "onCommit")
			this._loaded = true;
		} else {
			console.debug("deconnect CropEditor");
			dojo.disconnect(this._editorEvent);
			dojo.disconnect(this._presetEvent);
			dojo.disconnect(this._commitEvent);
			this.regionSelector.destroy();
			console.debug("regionSelecotr destroyed");
		}
	},
	
	valuesLoaded: function() {
		if (ctrl.module == "crop") {
			this.regionSelector.min = ctrl.values.min
			this.regionSelector.max = ctrl.values.max
			this.regionSelector.holdAspect = ctrl.values.holdAspect
			this.regionSelector.sizeable = ctrl.values.sizeable
			this.regionSelector.setCoords(ctrl.values.coords)
			this.regionSelector.setup()
		}
	},		
	
	onCommit: function() {
		console.debug("CropEditor onCommit");
		ctrl.commit(this.getValues())
	},
	
	getValues: function() {
		return this.regionSelector.getCoords()
	},
	
	
});

dojo.addOnLoad( function () {
	//dojo.parser.parse();
	cropper = new CropEditor();
});
